//
//  WiiKit.m
//  iFeller
//
//  Created by Wii on 15/12/12.
//  Copyright © 2015年 Wii. All rights reserved.
//

#import "WiiKit.h"

typedef NS_ENUM (NSInteger,WiiWeek){
    WiiWeekSunday = 1,
    WiiWeekMonday ,
    WiiWeekTuesday ,
    WiiWeekWednesday ,
    WiiWeekThursday ,
    WiiWeekFriday ,
    WiiWeekSaturday
};

@interface WiiKit()
@property (nonatomic , assign) WiiPresentTransition type;
// 毛玻璃视图
@property (nonatomic , strong) UIVisualEffectView *effectView;
@end
@implementation WiiKit

+ (NSString *)getCurrenDateForSystem{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"YYYY/MM/dd"];
    return [dateFormatter stringFromDate:[NSDate date]];
}

+ (NSString *)getCurrenTimeForSystem{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"YYYY年MM月dd日 HH:mm"];
    return [dateFormatter stringFromDate:[NSDate date]];
}

// 获取当前是星期几
+ (NSString *)getNowWeekday {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday |
    NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDate *now = [NSDate date];
    // 话说在真机上需要设置区域，才能正确获取本地日期，天朝代码:zh_CN
    calendar.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    comps = [calendar components:unitFlags fromDate:now];
    switch ([comps weekday]) {
        case WiiWeekMonday:
            return @"星期一";
            break;
        case WiiWeekTuesday:
            return @"星期二";
            break;
        case WiiWeekWednesday :
            return @"星期三";
            break;
        case WiiWeekThursday:
            return @"星期四";
            break;
        case WiiWeekFriday:
            return @"星期五";
            break;
        case WiiWeekSaturday:
            return @"星期六";
            break;
        case WiiWeekSunday:
            return @"星期天";
            break;
        default:
            return @"";
            break;
    }
//    return 0;
}

+ (NSString *)getCurrenUserTable{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"YYYYMMdd"];
    return [NSString stringWithFormat:@"user%@",[dateFormatter stringFromDate:[NSDate date]]];
}

#pragma mark - 隐藏NavigationBar底部横线
+ (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}
#pragma mark - Cell Animation
+ (void)mainTableViewCellAnimation:(UITableViewCell *)cell{
    
    CATransform3D rotation;
    rotation = CATransform3DMakeRotation( (90.0*M_PI)/180, 0.0, 0.7, 0.4);
    rotation.m34 = 1.0/ -600;
    
    cell.layer.shadowColor = [[UIColor blackColor]CGColor];
    cell.layer.shadowOffset = CGSizeMake(10, 10);
    cell.alpha = 0;
    cell.layer.transform = rotation;
    cell.layer.anchorPoint = CGPointMake(0, 0.5);
    
    [UIView beginAnimations:@"rotation" context:NULL];
    [UIView setAnimationDuration:0.8];
    cell.layer.transform = CATransform3DIdentity;
    cell.alpha = 1;
    cell.layer.shadowOffset = CGSizeMake(0, 0);
    [UIView commitAnimations];
}

#pragma mark - UIAlertAction (错误提示)
+ (UIAlertController *)alertControllerWithMessage:(NSString *)message{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"⚠️提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alert addAction:action];
    return alert;
}

#pragma mark － 转场动画

+ (instancetype)transitionWithTransitionType:(WiiPresentTransition)type{
    return [[self alloc] initWithTransitionType:type];
}
- (instancetype)initWithTransitionType:(WiiPresentTransition)type{
    self = [super init];
    if (self) {
        _type = type;
    }
    return self;
}
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    return _type == WiiPresentTransitionPresent ? 1:0.5;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
    switch (_type) {
        case WiiPresentTransitionPresent:{
            [self presentAnimation:transitionContext];
        }
            
            break;
        case WiiPresentTransitionDismiss:{
            [self dismissAnimation:transitionContext];
        }
            
            break;
    }
}
// 动画实现
- (void)presentAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
    //通过viewControllerForKey取出2个VC ,comeToVC 是当前VC, comeFromVC 是将要跳转的VC；
    UIViewController *comeToVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController *comeFromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    //对comeFrom截图 ， 根据截图动作做动画
    UIView *cutView = [comeFromVC.view snapshotViewAfterScreenUpdates:NO];
    cutView.frame = comeFromVC.view.frame;
    comeFromVC.view.hidden = YES;
    // 实现模糊效果
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    self.effectView = [[UIVisualEffectView alloc] initWithEffect:blur];
    self.effectView.frame = cutView.frame;
    self.effectView.alpha = 1;
    self.effectView.userInteractionEnabled = YES;
    [cutView addSubview:self.effectView];
    
    UIView *containerView = [transitionContext containerView];
    // 添加截图和跳转前的视图
    [containerView addSubview:cutView];
    [containerView addSubview:comeToVC.view];
    comeToVC.view.frame = CGRectMake(WiiZero, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT / 2);
    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 usingSpringWithDamping:0.55 initialSpringVelocity:1 / 0.55 options:0 animations:^{
        comeToVC.view.transform = CGAffineTransformMakeTranslation(0, -(SCREEN_HEIGHT - SCREEN_HEIGHT / 4));
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
//        [self.effectView removeFromSuperview];
        //转场失败后的处理
        if ([transitionContext transitionWasCancelled]) {
            //失败后，把vc1显示出来
            comeFromVC.view.hidden = NO;
            //然后移除截图视图，因为下次触发present会重新截图
            [cutView removeFromSuperview];
        }
    }];
}
// dismiss 动画
- (void)dismissAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
    UIViewController *comeToVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController *comeFromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIView *cutView = [transitionContext containerView].subviews[0];
    
    [UIView animateWithDuration:0.5 animations:^{
        comeFromVC.view.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        if ([transitionContext transitionWasCancelled]) {
            [transitionContext completeTransition:NO];
        } else
        {
            [transitionContext completeTransition:YES];
            
            comeToVC.view.hidden = NO;
            [cutView removeFromSuperview];
        }
    }];
}
@end
